import React, {PureComponent} from "react";
import {connect} from "react-redux";
import {Form} from "antd";
import {query} from "./actions";
import FilterForm from "../../components/FilterForm";
import {getCurrentOrganization, getLastFilter, getPrincipal, saveLastFilter} from "../../lib/identity";
const LAST_FILTER = "client";

class Filter extends PureComponent {
    state = {};
    handleSearch = (values) => {
        const {dispatch, pageSize} = this.props;
        let fields = values;
        this.setState({...fields});
        saveLastFilter(LAST_FILTER, {...fields});
        dispatch(query({
            ...fields,
            "userId": getPrincipal().id,
            "currentOrgId": getCurrentOrganization(),
            page: 1,
            pageSize
        }));
    };

    render() {
        const {loading} = this.props;
        let fields = getLastFilter(LAST_FILTER);
        if (fields === null || fields === undefined) {
            fields = {};
        }
        const filterSchema = [
            {
                key: 'code',
                field: 'code',
                type: 'text',
                expandable: true,
                width: 200,
                title: '客户编号',
                fieldOptions: {
                    initialValue: this.state.code || fields.code
                }
            }, {
                key: 'name',
                field: 'name',
                type: 'text',
                expandable: true,
                width: 200,
                title: '客户简称',
                fieldOptions: {
                    initialValue: this.state.name || fields.name
                }
            }, {
                key: 'fullName',
                field: 'fullName',
                type: 'text',
                expandable: true,
                width: 200,
                title: '客户全称',
                fieldOptions: {
                    initialValue: this.state.fullName || fields.fullName
                }
            }
        ];

        return (
            <FilterForm schema={filterSchema} callback={this.handleSearch} loading={loading}/>
        );
    }
}

const mapStateToProps = state => {
    return {
        ...state.client.list
    };
};

export default connect(mapStateToProps)(Form.create()(Filter));
